package com.hzit.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * 类名：
 * 作者：WF
 * 功能：自定义redis的序列化机制
 */
// @Configuration
public class RedisConfig {

	@Bean
	public RedisTemplate redisTemplate(RedisConnectionFactory factory){
		RedisTemplate redisTemplate = new RedisTemplate();
		redisTemplate.setConnectionFactory(factory);
		//1. 设置key的序列化器
		redisTemplate.setKeySerializer(new StringRedisSerializer());

		//2. 设置value的序列化器
		//方法一：
		// redisTemplate.setValueSerializer(new GenericFastJsonRedisSerializer());
		//方法二：
		redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
		return redisTemplate;
	}
}
